﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
/// <summary>
/// Summary description for DAL_Advertise
/// </summary>
public class DAL_Advertise
{
    DbProvider db = new DbProvider();

    public void Advertise_Insert(DTO_Advertise obj)
    {

        string sql = string.Format("INSERT INTO [Advertise] VALUES (N'{0}',N'{1}',N'{2}',N'{3}','{4}','{5}','{6}','{7}','{8}')", obj.Name, obj.Link, obj.Images, obj.Target, obj.Position, obj.Order, obj.DateStar, obj.DateEnd, obj.Status);

        try
        {
            db.ExecuteNonQuery(sql);
        }
        catch (Exception)
        {

            throw;
        }
     

    }
    public void Advertise_UpdateByAll(string Id, DTO_Advertise obj)
    {

        //string sql = string.Format("UPDATE [Advertise] SET [Name] = '{0}',Link = '{1}',[Images] = '{2}',[Width] = '{3}',[Height] = {4}',[Target] = '{5}',[Position] = {6}',[Order] = '{7}',[Status] = '{8}' WHERE [Id] = '{9}'", obj.Name, obj.Link, obj.Images, obj.Width, obj.Height, obj.Target, obj.Position, obj.Order, obj.Status, Id);
        string sql = "UPDATE [Advertise] SET";
        if (obj.Name != "") sql += " [Name] = N'"+obj.Name+"',";
        
        if (obj.Link != "") sql += " [Link] = '" + obj.Link + "',";
        if (obj.Images != "") sql += " [Image] = '" + obj.Images + "',";
        
        if (obj.Target != "") sql += " [Target] = '" + obj.Target + "',";
        if (obj.Position != 0) sql += " [Position] = '" + obj.Position + "',";
        if (obj.DateStar != "") sql += " [DateStar] = '" + obj.DateStar + "',";
        if (obj.DateEnd != "") sql += " [DateEnd] = '" + obj.DateEnd + "',";
        if (obj.Order != 0) sql += " [Order] = '" + obj.Order + "',";
        sql += " [Status] = '"+obj.Status+"' where Id = '"+Id+"'";
        try
        {
            db.ExecuteNonQuery(sql);
        }
        catch (Exception)
        {

            throw;
        }

    }

    public void Advertise_UpdateBySet(string Id, string Set)
    {

        string sql = string.Format("UPDATE [Advertise] SET {0} WHERE [Id] = '{1}'", Set, Id);

        try
        {
            db.ExecuteNonQuery(sql);
        }
        catch (Exception)
        {

            throw;
        }

    }
    public void Advertise_Delete(string Id)
    {

        string sql = string.Format("DELETE FROM Advertise WHERE Id = '{0}'", Id);

        try
        {
            db.ExecuteNonQuery(sql);
        }
        catch (Exception)
        {

            throw;
        }

    }

    public DataTable Advertise_GetByAll()
    {
        DataTable dt = new DataTable();
        string sql = "select * from Advertise";
        try
        {
            dt = db.ExecuteQuery(sql);

        }
        catch (Exception)
        {

            throw;
        }
        return dt;
    }
    public DataTable Advertise_GetByTop(int top, string where, string order)
    {
        DataTable dt = new DataTable();
        //string sql = string.Format("select top {0} * from Product where = '{1}' Order by [{2}] DESC,[Order]", top, where,order);
        string sql = "";
        if (top <= 0)
        {
            sql += "SELECT * FROM Advertise";
        }
        else
        {
            sql += "SELECT TOP " + top + " * FROM Advertise";
        }
        if (where != "")
        {
            sql += " WHERE " + where;
        }
        if (order != "")
        {
            sql += " Order by " + order;
        }
        try
        {
            dt = db.ExecuteQuery(sql);

        }
        catch (Exception)
        {

            throw;
        }
        return dt;
    }
    public DataTable Advertise_GetByID(string id)
    {
        DataTable dt = new DataTable();
        string sql = string.Format("select * from Advertise where Id = '{0}'", id);
        try
        {
            dt = db.ExecuteQuery(sql);

        }
        catch (Exception)
        {

            throw;
        }
        return dt;
    }
}